package net.aiion.weave.impl.bytecode;

import net.aiion.weave.asm.AnnotationVisitor;
import net.aiion.weave.spi.bytecode.BytecodeAnnotationVisitor;
import net.aiion.weave.spi.bytecode.BytecodeClassVisitor;
import net.aiion.weave.spi.bytecode.BytecodeFieldVisitor;
import net.aiion.weave.spi.bytecode.BytecodeMethodVisitor;
import net.aiion.weave.spi.bytecode.events.AnnotationAnnotationValueBytecodeEvent;
import net.aiion.weave.spi.bytecode.events.AnnotationArrayValueBytecodeEvent;
import net.aiion.weave.spi.bytecode.events.AnnotationEndBytecodeEvent;
import net.aiion.weave.spi.bytecode.events.AnnotationEnumValueBytecodeEvent;
import net.aiion.weave.spi.bytecode.events.AnnotationValueBytecodeEvent;

public class BytecodeAnnotationWriterVisitor implements BytecodeAnnotationVisitor
{
    private final AnnotationVisitor annotationVisitor;

    public BytecodeAnnotationWriterVisitor(final AnnotationVisitor aAnnotationVisitor) {
        annotationVisitor = aAnnotationVisitor;
    }

    @Override
    public BytecodeClassVisitor asBytecodeClassVisitor() {
        return null;
    }

    @Override
    public BytecodeAnnotationVisitor asBytecodeAnnotationVisitor() {
        return this;
    }

    @Override
    public BytecodeFieldVisitor asBytecodeFieldVisitor() {
        return null;
    }

    @Override
    public BytecodeMethodVisitor asBytecodeMethodVisitor() {
        return null;
    }

    @Override
    public void visitAnnotationValue(final AnnotationValueBytecodeEvent aAnnotationValueBytecodeEvent) {
        annotationVisitor.visit(aAnnotationValueBytecodeEvent.getName(), aAnnotationValueBytecodeEvent.getValue());
    }

    @Override
    public void visitAnnotationEnumValue(final AnnotationEnumValueBytecodeEvent aAnnotationEnumValueBytecodeEvent) {
        annotationVisitor.visitEnum(aAnnotationEnumValueBytecodeEvent.getName(),
            aAnnotationEnumValueBytecodeEvent.getDescriptor(), aAnnotationEnumValueBytecodeEvent.getValue());
    }

    @Override
    public BytecodeAnnotationVisitor visitAnnotationAnnotationValue(
        final AnnotationAnnotationValueBytecodeEvent aAnnotationAnnotationValueBytecodeEvent)
    {
        return new BytecodeAnnotationWriterVisitor(annotationVisitor.visitAnnotation(
            aAnnotationAnnotationValueBytecodeEvent.getName(), aAnnotationAnnotationValueBytecodeEvent.getDescriptor()));
    }

    @Override
    public BytecodeAnnotationVisitor visitAnnotationArrayValue(
        final AnnotationArrayValueBytecodeEvent aAnnotationArrayValueBytecodeEvent)
    {
        return new BytecodeAnnotationWriterVisitor(annotationVisitor.visitArray(aAnnotationArrayValueBytecodeEvent
            .getName()));
    }

    @Override
    public void visitAnnotationEnd(final AnnotationEndBytecodeEvent aAnnotationEndBytecodeEvent) {
        annotationVisitor.visitEnd();
    }
}
